package cn.lysy.xiaoruan;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Window;

import cn.lysy.xiaoruan.activity.HomeActivity;
import cn.lysy.xiaoruan.base.BaseActivity;
import cn.lysy.xiaoruan.fragment.MainFragment;
import cn.lysy.xiaoruan.fragment.MineFragment;

public class MainActivity extends BaseActivity {
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;

    private MainFragment mainFragment;
    private MineFragment mineFragment;

    private SharedPreferences preferences;
    private final int mode=MODE_PRIVATE;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        preferences=getSharedPreferences("xiaoruan",mode);

        fragmentManager=getSupportFragmentManager();

        showMainFragment();

    }

    @Override
    protected void onResume() {
        int loginid = getIntent().getIntExtra("loginid",0);

        switch( loginid ){
            case 1 :
                Intent intent=new Intent(MainActivity.this, HomeActivity.class);
                startActivity(intent);
            default : break;
        }
        super.onResume();
    }

    public void showMainFragment() {
        fragmentTransaction=
                fragmentManager.beginTransaction();
        mainFragment=new MainFragment();
        fragmentTransaction.add(R.id.fragment_holder,mainFragment);
        fragmentTransaction.commit();
    }


    public void  replaceLoginFragment(){
        fragmentTransaction=
                fragmentManager.beginTransaction();
        mainFragment=new MainFragment();
        fragmentTransaction.replace(R.id.fragment_holder,mainFragment);
        fragmentTransaction.commit();
    }

    private void replace( Fragment fragment ){
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace( R.id.fragment_holder , fragment );
        fragmentTransaction.commit();
    }
}
